package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.domain.SignInHistory;
import cn.wolfcode.trip.base.service.ISignInHistoryService;
import cn.wolfcode.trip.base.service.ISignInService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 签到控制器
 */
@RestController
@RequestMapping("signIns")
public class SignInController {
    @Autowired
    private ISignInService signInService;
    @Autowired
    private ISignInHistoryService signInHistoryService;

    /**
     * 根据用户id,和当天日期,获取签到对象的接口
     * 1.资源 /signIns/{id}
     * 2.请求方式 post
     * 3.请求参数
     * 4.返回结果 SingIn
     */
    //只判断签到状态
    @GetMapping("{id}")
    public SignIn getByIdAndDate(@PathVariable Long id) throws ParseException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(date);
        //今天的日期解析
        Date date1 = sdf.parse(today);
        return signInService.getByIdAndDate(id,date1);
    }


    /**
     * 根据用户id,获取签到对象的接口
     * 1.资源 /signIns/{id}
     * 2.请求方式 get
     * 3.请求参数
     * 4.返回结果 SingIn
     */
    //签到按钮点击后执行
    @PostMapping("{id}")
    public SignIn saveOrUpdate(@PathVariable Long id) throws ParseException {
        signInService.saveOrUpdate(id);
        SignInHistory signInHistory = new SignInHistory();
        signInHistory.setUser_id(id);
        signInHistoryService.save(signInHistory);
       return signInService.getById(id);
    }



}
